package com.shixun.controller;


import com.shixun.exception.exception.DormException;
import com.shixun.service.DormitoryService;
import com.shixun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 给学生分配宿舍
 * </p>
 *
 * @author shixun
 * @since 2023-06-10
 */
@RestController
@RequestMapping("/dormitory")
public class DormitoryController {
    @Autowired
    private DormitoryService dormitoryService;

    /**
     * 给没有宿舍的学生一键自动分配宿舍
     */
    @PostMapping("assignment")
    @CacheEvict(value = "studentMsg",allEntries = true)
    public R assignment(){
       return dormitoryService.assignment();
    }

    /**
     * 学生查看自己的宿舍
     */
    @GetMapping("searchDorm")
    public R searchDorm(HttpSession session){
      String sno= (String) session.getAttribute("student");
      if (sno==null){
          throw new DormException("请先登陆");
      }
      return dormitoryService.searchDorm(sno);

    }


}

